package com.corpgovernment.organization.mapper;

import com.corpgovernment.organization.common.mybatis.TkMapper;
import com.corpgovernment.organization.entity.db.MbUserOrgRelation;
import com.ctrip.corp.obt.generic.notification.annotation.InfoChange;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author wangyujue
 */
@Repository
public interface MbUserOrgRelationMapper extends TkMapper<MbUserOrgRelation> {

    int countEmployee(@Param("status") Integer status, @Param("orgIds") List<String> orgIds);

    List<MbUserOrgRelation> listByUids(@Param("uids") List<String> employeeUidList);

    List<Map<Long, Long>>  groupByPostId(@Param("postIds") List<Long> postIds);

    List<MbUserOrgRelation> listAllByUids(@Param("uids") List<String> employeeUidList);

    MbUserOrgRelation selectOneByUid(@Param("uid")String uid);


    /**
     * 批量保存
     *
     * @param needSaveList 需要插入的数据
     * @return 影响的行数
     */
    int batchSave(@Param("needSaveList") List<MbUserOrgRelation> needSaveList);

    int batchUpdateStauts(@Param("uids") List<String> uids, @Param("orgId") String orgId, @Param("type") Integer type);

    /**
     * 根据uid数组和orgId批量更新岗位
     *
     * @param postId 岗位id
     * @param uids   uid列表
     * @param orgId  组织id
     * @return
     */
    int batchUpdatePostByUidAndOrgId(Long postId, List<String> uids, String orgId);

    /**
     * 批量清除管理员
     *
     * @param ids
     * @param orgId
     * @return
     */
    int batchClearLeader(@Param("ids") List<String> ids, @Param("orgId") String orgId);

    /**
     * 设置管理员
     *
     * @param orgId
     * @param uids
     * @param isLeader 0：非管理员，1：管理员
     * @return
     */
    int setUpLeader(@Param("orgId") String orgId, @Param("uids") List<String> uids, @Param("isLeader") Integer isLeader);

    /**
     * 根据postId清除人员岗位信息
     *
     * @param postId
     * @return
     */
    int cleanPostIdByPostId(Long postId);

    /**
     * 根据uid和orgId清除人员岗位信息
     *
     * @param uid
     * @return
     */
    int cleanPostIdByUidAndOrgId(@Param("uid") String uid, @Param("orgId") String orgId);

    /**
     * 根据uid和orgId清除人员岗位信息
     *
     * @param uid
     * @param orgId
     * @param postId
     * @return
     */
    int setPostIdByUidAndOrgId(@Param("uid") String uid, @Param("orgId") String orgId, @Param("postId") Long postId);

    int deleteByOrgIdAndUid(@Param("uid") String uid, @Param("orgId") String orgId);

    /**
     * 根据uid删除用户
     *
     * @param uid
     * @return
     */
    int deleteByUid(@Param("uid")String uid);

    /**
     * 查询指定组织下的员工组织关系
     *
     * @param orgIdList
     * @return
     */
    List<MbUserOrgRelation> listByOrgIds(@Param("orgIdList") List<String> orgIdList);

    @InfoChange(group = "VALID_STAFF", identityExpression = "uid", paramLocation = 2)
    int updateUserOrgRelation(@Param("orgId") String orgId, @Param("oldOrgId") String oldOrgId, @Param("uid") String uid,@Param("isLeader") Boolean isLeader, @Param("postId") Long postId);

    /**
     * 查询一个员工所有的职级
     *
     * @param uid
     * @return
     */
    List<MbUserOrgRelation> listByUid(@Param("uid") String uid);

    int updatePostIdById(@Param("postId") Long postId, @Param("id") Long id);

    int deleteByUidAndOrgIdList(@Param("mbUserOrgRelationList") List<MbUserOrgRelation> mbUserOrgRelationList);

    @MapKey(value = "orgId")
	Map<String, Map<String, Long>> countUidByOrgIds(@Param("orgIds") List<String> orgIds);

    /**
     * 查询指定组织下的员工组织关系
     *
     * @param orgIdList
     * @return
     */
    List<MbUserOrgRelation> listByOrgIdsNew(@Param("orgIdList") List<String> orgIdList);
}
